package com.andova.component.net.converter.gson;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.Excluder;
import com.google.gson.internal.bind.TypeAdapters;

import java.lang.reflect.Type;
import java.util.Collections;

/**
 * Created by Administrator on 2019-01-31.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public final class GsonBeanFactory {
    public static Gson newGson() {
        return new GsonBuilder()
                // 注册String类型处理器
                .registerTypeAdapterFactory(TypeAdapters.newFactory(String.class, GsonTools.stringTypeAdapter()))
                // 注册int.class, Integer.class处理器
                .registerTypeAdapterFactory(TypeAdapters.newFactory(int.class, Integer.class, GsonTools.longAdapter(0)))
                // 注册short.class, Short.class处理器
                .registerTypeAdapterFactory(TypeAdapters.newFactory(short.class, Short.class, GsonTools.longAdapter(1)))
                // 注册long.class, Long.class处理器
                .registerTypeAdapterFactory(TypeAdapters.newFactory(long.class, Long.class, GsonTools.longAdapter(2)))
                // 注册double.class, Double.class处理器
                .registerTypeAdapterFactory(TypeAdapters.newFactory(double.class, Double.class, GsonTools.longAdapter(3)))
                // 注册float.class, Float.class处理器
                .registerTypeAdapterFactory(TypeAdapters.newFactory(float.class, Float.class, GsonTools.longAdapter(4)))
                // 注册反射对象的处理器
                .registerTypeAdapterFactory(new ReflectiveTypeAdapterFactory(new ConstructorConstructor(Collections.<Type, InstanceCreator<?>>emptyMap()), FieldNamingPolicy.IDENTITY, Excluder.DEFAULT))
                // 注册集合的处理器
                .registerTypeAdapterFactory(new CollectionTypeAdapterFactory(new ConstructorConstructor(Collections.<Type, InstanceCreator<?>>emptyMap())))
                .create();
    }
}
